package com.hangzhou.xyj.ui.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.XYJApplication;
import com.hangzhou.xyj.db.preferences.CommonPreferences;
import com.hangzhou.xyj.event.OperationRespEvent;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.CommentModel;
import com.hangzhou.xyj.model.CommentPageInfoModel;
import com.hangzhou.xyj.model.DynamicDetailModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.net.handler.GetCommentListHandler;
import com.hangzhou.xyj.net.handler.GetDynamicDetailHandler;
import com.hangzhou.xyj.net.handler.MyLikeHandler;
import com.hangzhou.xyj.net.handler.ReportHandler;
import com.hangzhou.xyj.net.handler.TransferHandler;
import com.hangzhou.xyj.net.http.HttpLoader;
import com.hangzhou.xyj.net.request.GetCommentListReq;
import com.hangzhou.xyj.net.request.GetDynamicDetailReq;
import com.hangzhou.xyj.net.request.MyLikeReq;
import com.hangzhou.xyj.net.request.ReportReq;
import com.hangzhou.xyj.net.request.TransferReq;
import com.hangzhou.xyj.ui.activity.base.BaseActivity;
import com.hangzhou.xyj.ui.adapter.DynamicDetailAdapter;
import com.hangzhou.xyj.ui.dialog.TipsDialog;
import com.hangzhou.xyj.ui.widget.views.MyTextView;
import com.hangzhou.xyj.utils.EventBusUtil;
import com.hangzhou.xyj.utils.PublicUtil;
import com.hangzhou.xyj.utils.ToastUtil;
import com.hangzhou.xyj.utils.constants.Constants;
import com.hangzhou.xyj.utils.constants.DataKey;

import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;


/**
 * Created by yaohuix on 2016/3/21  11:41
 * <p/>
 * 说明：动态详情
 */
public class DynamicDetailActivity extends BaseActivity implements View.OnClickListener {
    RecyclerView mRecyclerView;
    DynamicDetailAdapter mAdapter;
    private List<CommentModel> mCommentList;
    private int id;//当前动态的id
    private String title;//标题
    private MyTextView mTvTransfer;//转发
    private MyTextView mTvComment;//评论
    private MyTextView mTvLike;//点赞
    private MyTextView mTvReport;//举报
    private int mIntentTag;
    private DynamicDetailModel mDetailModel;


    @Override
    public void initDatas() {
        //请求数据
        this.getDynamicDetail();
        this.getCommentList(1);
    }

    @Override
    public void initIntentData(Intent intent) {
        EventBusUtil.register(this);
        id = intent.getIntExtra(DataKey.INDEX, -1);
        title = intent.getStringExtra(DataKey.TITLE);
        mIntentTag = intent.getIntExtra(DataKey.INTENT_TAG, -1);
    }

    @Override
    public void initTop() {
        enableTitle(true, title);
    }

    @Override
    public void initListeners() {
        mTvTransfer.setOnClickListener(this);
        mTvComment.setOnClickListener(this);
        mTvLike.setOnClickListener(this);
        mTvReport.setOnClickListener(this);
        mAdapter.setOnNumChangeListener(new DynamicDetailAdapter.OnNumChangeListener() {
            @Override
            public void onAddChange(int type) {
                //点赞
                if (type == LIKE_TYPE) {
                    if (mDetailModel != null) {
                        mDetailModel.setLikeNum(mDetailModel.getLikeNum() + 1);
                    }
                } else if (type == COMMENT_TYPE) {
                    if (mDetailModel != null) {
                        mDetailModel.setCommentNum(mDetailModel.getCommentNum() + 1);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onDeleteChange(int type) {
                //点赞
                if (type == LIKE_TYPE) {
                    if (mDetailModel != null) {
                        mDetailModel.setLikeNum(mDetailModel.getLikeNum() - 1);
                    }
                } else if (type == COMMENT_TYPE) {
                    if (mDetailModel != null) {
                        mDetailModel.setCommentNum(mDetailModel.getCommentNum() - 1);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void initViews() {
        mRecyclerView = findView(R.id.rv_content);
        mTvTransfer = findView(R.id.mtv_transfer);
        mTvComment = findView(R.id.mtv_comment);
        mTvLike = findView(R.id.mtv_like);
        mTvReport = findView(R.id.mtv_report);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new DynamicDetailAdapter(this);
        mAdapter.setIntentTag(mIntentTag);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void initLayouts() {
        setContentView(R.layout.activity_dynamic_detail);
    }

    /**
     * 获取评论列表
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/22 10:07
     */
    public void getCommentList(int currentPage) {
        GetCommentListReq getCommentListReq = new GetCommentListReq(String.valueOf(id), 2, Constants.PAGE_SIZE, currentPage);
        HttpLoader.getInstance().sendRequest(new GetCommentListHandler(getCommentListReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                CommentPageInfoModel pageInfoModel = (CommentPageInfoModel) model;
                mCommentList = pageInfoModel.getCommentList();
                mAdapter.updateCommentList(mCommentList);
            }
        }, 0));
    }

    /**
     * 获取动态详情
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/22 10:15
     */
    public void getDynamicDetail() {
        GetDynamicDetailReq getDynamicDetailReq;
        if (XYJApplication.getUserInfo() == null) {
            getDynamicDetailReq = new GetDynamicDetailReq(id);
        } else {
            getDynamicDetailReq = new GetDynamicDetailReq(id, Integer.valueOf(CommonPreferences.getUserId(mContext)));
        }
        HttpLoader.getInstance().sendRequest(new GetDynamicDetailHandler(getDynamicDetailReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                mDetailModel = (DynamicDetailModel) model;
                mAdapter.updateBaseCommentInfo(mDetailModel);
            }
        }, 0));
    }


    @Override
    public void onClick(View v) {
        //未登录
        if (XYJApplication.getUserInfo() == null) {
            new TipsDialog(mContext, "提示", "需要登录才能操作，是否登录？", "确定", "取消") {
                @Override
                protected void OnClickNegativeButton() {
                    this.dismiss();
                }

                @Override
                protected boolean OnClickPositiveButton() {
                    IntentManager.getInstance().goLoginActivity(mContext);
                    return true;
                }

            }.show();
            return;
        }
        switch (v.getId()) {
            case R.id.mtv_transfer:
                PublicUtil.showShareVersion(new PlatformActionListener() {
                    @Override
                    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                        afterTransFer(id, 2);
                    }

                    @Override
                    public void onError(Platform platform, int i, Throwable throwable) {

                    }

                    @Override
                    public void onCancel(Platform platform, int i) {

                    }
                });
                break;
            case R.id.mtv_like:
                like();
                break;
            case R.id.mtv_comment:
                IntentManager.getInstance().goCommentActivity(mContext, id, EIntentTagType.TAG_DYNAMIC_DETAIL);
                break;
            case R.id.mtv_report:
                new TipsDialog(mContext, "提示", "确定举报？", "确定", "取消") {
                    @Override
                    protected void OnClickNegativeButton() {
                        this.dismiss();
                    }

                    @Override
                    protected boolean OnClickPositiveButton() {
                        report();
                        return true;
                    }

                }.show();
                break;
            default:
                break;
        }
    }

    /**
     * 点赞
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/23 14:22
     */
    public void like() {
        MyLikeReq myLikeReq = new MyLikeReq(MyLikeReq.TYPE_DYNAMIC, id, Integer.valueOf(CommonPreferences.getUserId(mContext)));
        HttpLoader.getInstance().sendRequest(new MyLikeHandler(myLikeReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("点赞成功!");
                    if (mDetailModel != null) {
                        mDetailModel.setLikeNum(mDetailModel.getLikeNum() + 1);
                        mAdapter.notifyDataSetChanged();
                    }
                }
            }
        }, 0));
    }

    /**
     * 举报
     *
     * @param
     * @return
     * @author yaohuix
     * @date 2016/3/24 14:50
     */
    public void report() {
        ReportReq reportReq = new ReportReq(id, 1, Integer.valueOf(CommonPreferences.getUserId(mContext)));
        HttpLoader.getInstance().sendRequest(new ReportHandler(reportReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if ((Boolean) model) {
                    ToastUtil.show("举报成功！");
                }
            }
        }, 0));
    }

    @Subscribe
    public void onEventMainThread(OperationRespEvent event) {
        if (event.isSuccess()) {
            getCommentList(mAdapter.getItemCount() / Constants.PAGE_SIZE + 1);
            getDynamicDetail();
        }
    }

    /**
     * 分享之后调用，用于统计分享的次数
     *
     * @param type  1：活动、演出，热点； 2：社群；
     * @param objId 被分享的对象的ID
     * @return
     * @author yaohuix
     * @date 2016/5/5 16:21
     */
    public void afterTransFer(int objId, int type) {
        TransferReq transferReq = new TransferReq(objId, type, mMyUserId);
        HttpLoader.getInstance().sendRequest(new TransferHandler(transferReq, new MyHttpResponseListener() {
            @Override
            public void onSuccess(Object model) {
                super.onSuccess(model);
                if (mDetailModel != null) {
                    mDetailModel.setTransferNum(mDetailModel.getTransferNum() + 1);
                    mAdapter.notifyDataSetChanged();
                }
            }
        }, 0));
    }

}
